<template>
  <el-dialog title="新增" @close="$emit('close')" :rules="rules" :visible.sync="dialogFormVisible">
    <el-form :model="user" ref="userForm" :rules="rules">
      <el-form-item label="用户名" prop="username" :label-width="formLabelWidth">
        <el-input v-model="user.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
        <el-input type="password" v-model="user.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword" :label-width="formLabelWidth">
        <el-input type="password" v-model="user.checkPassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别" :label-width="formLabelWidth">
        <el-radio v-model="user.sex" :label="0">男</el-radio>
        <el-radio v-model="user.sex" :label="1">女</el-radio>
      </el-form-item>
      <el-form-item label="年龄" :label-width="formLabelWidth">
        <el-input-number v-model="user.age" :min="1" :max="100" label="描述文字"></el-input-number>
      </el-form-item>
      <el-form-item label="生日" :label-width="formLabelWidth">
        <el-date-picker v-model="user.birthday" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="地址" :label-width="formLabelWidth">
        <el-input v-model="user.address" type="textarea" autocomplete="off"></el-input>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {signin} from '@/api/user'
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"))
      } else {
        if (this.user.checkPassword !== "") {
          this.$refs.userForm.validateField("checkPass")
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"))
      } else if (value !== this.user.password) {
        callback(new Error("两次输入密码不一致!"))
      } else {
        callback()
      }
    }

    return {
      formLabelWidth: "80px",
      dialogFormVisible: true,
      user: {
        username: null,
        sex: 1,
        age: 1,
        birthday: "",
        address: "",
        password: "",
        checkPassword: ""
      },
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空",
            trigger: "blur"
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur"
          }
        ],
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPassword: [{ validator: validatePass2, trigger: "blur" }]
      }
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          let params = Object.assign({},this.user)
          delete params['checkPassword']
          let rs = await signin(params)
          console.log("signin:", rs)
          if (rs.code === 1) {
            this.$emit("close", "success")
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  text-align: left;
}
.el-form-item /deep/ .el-form-item__label {
  text-align: center;
}
</style>


